package com.zdsoft.finance.meet.repository;

import java.math.BigDecimal;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.zdsoft.finance.common.utils.AmountConversionUtil;
import com.zdsoft.finance.meet.entity.MeetingProject;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 上会项目Repository实现类
 * 
 * @author liuwei
 *
 */
public class MeetingProjectRepositoryImpl {

	@PersistenceContext
	private EntityManager em;

	/**
	 * 通过conditions查询分页上会项目
	 * 
	 * @param pageable
	 *            分页信息
	 * @param conditions
	 *            查询条件
	 * @return 分页上会项目
	 */
	public Page<MeetingProject> findByConditions(Pageable pageable, Map<String, Object> conditions) {
		StringBuffer hql = new StringBuffer(
				" select mt from MeetingProject as mt inner join mt.meetingMembers mm  where mt.isDeleted = 0 and mm.isDeleted = 0 ");
		StringBuffer whereHql = new StringBuffer();
		if (ObjectHelper.isNotEmpty(conditions.get("projectCode"))) {
			whereHql.append(" and mt.project.projectCode like :projectCd ");
		}
		if (ObjectHelper.isNotEmpty(conditions.get("projectNm"))) {
			whereHql.append(" and mt.project.projectNm like :projectNm ");
		}
		if (ObjectHelper.isNotEmpty(conditions.get("clientName"))) {
			whereHql.append(" and mt.project.clientName like :clientName ");
		}
		if (ObjectHelper.isNotEmpty(conditions.get("amount"))) {
			String[] amount = ((String) conditions.get("amount")).split(",");
			if (ObjectHelper.isNotEmpty(amount)) {
				if (ObjectHelper.isNotEmpty(amount[0])) {
					whereHql.append(" and mt.project.amount >= :amountBegin ");
				}
				if (amount.length > 1 && ObjectHelper.isNotEmpty(amount[1])) {
					whereHql.append(" and mt.project.amount <= :amountEnd ");
				}
			}
		}
		if (ObjectHelper.isNotEmpty(conditions.get("belongOrgNm"))) {
			whereHql.append(" and mt.project.belongOrgNm like :belongOrgNm ");
		}
		if (ObjectHelper.isNotEmpty(conditions.get("empCd"))) {
			whereHql.append(" and mm.code = :empCd ");
		}

		Query query = em.createQuery(hql.toString() + whereHql.toString());
		Query countQ = em.createQuery(
				"select count(*) from MeetingProject as mt inner join mt.meetingMembers mm  where mt.isDeleted = 0 and mm.isDeleted = 0 "
						+ whereHql.toString());

		if (ObjectHelper.isNotEmpty(conditions.get("projectCode"))) {
			query.setParameter("projectCd", conditions.get("projectCode"));
			countQ.setParameter("projectCd", conditions.get("projectCode"));
		}
		if (ObjectHelper.isNotEmpty(conditions.get("projectNm"))) {
			query.setParameter("projectNm", conditions.get("projectNm"));
			countQ.setParameter("projectNm", conditions.get("projectNm"));
		}
		if (ObjectHelper.isNotEmpty(conditions.get("clientName"))) {
			query.setParameter("clientName", conditions.get("clientName"));
			countQ.setParameter("clientName", conditions.get("clientName"));
		}
		if (ObjectHelper.isNotEmpty(conditions.get("amount"))) {
			String[] amount = ((String) conditions.get("amount")).split(",");
			if (ObjectHelper.isNotEmpty(amount)) {
				if (ObjectHelper.isNotEmpty(amount[0])) {
					query.setParameter("amountBegin",
							AmountConversionUtil.convertToYuan(new BigDecimal(amount[0])));
					countQ.setParameter("amountBegin",
							AmountConversionUtil.convertToYuan(new BigDecimal(amount[0])));
				}
				if (amount.length > 1 && ObjectHelper.isNotEmpty(amount[1])) {
					query.setParameter("amountEnd",
							AmountConversionUtil.convertToYuan(new BigDecimal(amount[0])));
					countQ.setParameter("amountEnd",
							AmountConversionUtil.convertToYuan(new BigDecimal(amount[0])));
				}
			}
		}
		if (ObjectHelper.isNotEmpty(conditions.get("belongOrgNm"))) {
			query.setParameter("belongOrgNm", conditions.get("belongOrgNm"));
			countQ.setParameter("belongOrgNm", conditions.get("belongOrgNm"));
		}
		if (ObjectHelper.isNotEmpty(conditions.get("empCd"))) {
			query.setParameter("empCd", conditions.get("empCd"));
			countQ.setParameter("empCd", conditions.get("empCd"));
		}
		// 查询
		query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
		query.setMaxResults(pageable.getPageSize());
		Page<MeetingProject> pager = new PageImpl<>(query.getResultList(), pageable, (Long) countQ.getSingleResult());
		return pager;
	}
}
